/**
 * @从一个字符串中裁减一个字符串
 * @看一下substring函数里面的错误
 * @author	 wangshikun <wangshikun2010@gmail.com>
 * @copyright	(c) 2012 wangshikun <wangshikun2010@gmail.com>
 * @license	http://www.opensource.org/licenses/mit-license.php The MIT License
 * @package	default
 * @subpackage	default
 * @TODO How to handle negative start, or count that is larger than strlen
 */

#include <stdio.h>
#include <cs50.h>

char *substring(const char source[], int start, int count);

int main(int argc, char *argv[]) {
	const char source[] = {"abcdefghijklmnopqrstuvwxyz"};
	int start, count;

	printf("source=%s\n", source);

	printf("Index to start: ");
	scanf("%d", &start);
	printf("Length to chop: ");
	scanf("%d", &count);

	printf("%s\n", substring(source, start, count));

	return 0;
}

char *substring(const char source[], int start, int count) {
	char *destination = malloc(sizeof(char) * (count+1));

	int i;
	for (int j=start, i=0; j<start + count; j++, i++) {
		*(destination + i) = source[j];
	}

	*(destination + i) = '\0';

	return destination;
}
